/*
 * @(#)SourceForgeInstructions.java   09/11/29
 *
 * Copyright (c) 2009 HKUST. All rights reserved.
 *
 */

/**
 *
 */
package org.ust.downloader;

//~--- non-JDK imports --------------------------------------------------------

import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

//~--- JDK imports ------------------------------------------------------------

import java.util.StringTokenizer;
import java.util.Vector;

/**
 * @author Nelson
 * 
 */
public class SourceForgeInstructions implements BrowseInstructions
{
	private String	InitialProjectListUrl;
	private String	downloadFolderName;

	/**
	 *
	 */
	public SourceForgeInstructions ( )
	{
		setInitialProjectListUrl ( "http://sourceforge.net/softwaremap/trove_list.php?form_cat=198" );
		setDownloadFolderName ( "APIs" );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ust.downloader.BrowseInstructions#analyzeFilePage(org.eclipse.swt
	 * .browser.Browser)
	 */

	/**
	 * 
	 * 
	 * @param downloadFolderName
	 */
	public SourceForgeInstructions ( String downloadFolderName )
	{
		// setInitialProjectListUrl (
		// "http://sourceforge.net/softwaremap/trove_list.php?form_cat=198" );
		setInitialProjectListUrl ( "http://sourceforge.net/softwaremap/trove_list.php?stquery=&sort=num_downloads&sortdir=desc&offset=650&form_cat=198" );
		setDownloadFolderName ( downloadFolderName );
	}

	/**
	 * Analyse file page using javascript dom library.
	 * 
	 * @return
	 */
	public String analyzeFilePage ( )
	{
		String jsScript = " function loadFileLinks() {"
				+ " var element =   document.getElementsByClassName('b-hornav')[0];"
				+ " if ( element != null ) {"
				+ "     var liTag = element.childNodes[3];"
				+ "     if (liTag != null) {"
				+ "         var aTag = liTag.childNodes[1];"
				+ "         if (aTag != null) {"
				+ "                return aTag.href;" + "          }"
				+ "     }" + "  }" + "  return null;" + "}"
				+ "return loadFileLinks();";

		return jsScript;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ust.downloader.BrowseInstructions#analyzeNewestFile(org.eclipse.swt
	 * .browser.Browser)
	 */

	/**
	 * Analyse the latest file using javascript dom library.
	 * 
	 * @return
	 */
	public String analyzeNewestFile ( )
	{
		String jsScript = " function loadLatestFileLinks() {"
				+ "     var tdElements  =   document.getElementsByTagName('td');"
				+ "     if ( tdElements != null ) {"
				+ "         var targetChild = null;"
				+ "         for (i=0; i< tdElements.length; i++) {"
				+ "             if (tdElements[i].innerHTML == 'Newest Files') {"
				+ "                 targetChild = tdElements[i];"
				+ "                 break;"
				+ "             }"
				+ "         }"
				+ "         if (targetChild != null) {"
				+ "             var targetParent = targetChild.parentNode.parentNode;"
				+ "             var downloadTargetParent = targetParent.childNodes[3];"
				+ "             var target = downloadTargetParent.childNodes[1].childNodes[1];"
				+ "				if (target == null || target == undefined) { "
				+ "					return null;"
				+ "				}"
				+ "             var fileName = target.innerHTML;"
				+ "             var href = target.href;"
				+ "             var indexOfDot = fileName.lastIndexOf('.');"
				+ "             var lengthOfStr = fileName.length;"
				+ "             var ext = fileName.substring(indexOfDot+1, lengthOfStr);"
				+ "             if (ext == 'zip' || ext == 'jar') {"
				+ "                 return href;" + "               }"
				+ "          }" + "      }" + " return null;" + "   }"
				+ "return loadLatestFileLinks();";

		return jsScript;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ust.downloader.BrowseInstructions#analyzeProjectLinks(java.lang.String
	 * )
	 */

	/**
	 * 
	 * @param projectLinks
	 * 
	 * @return
	 */
	public Vector < String > analyzeProjectLinks ( String projectLinks )
	{
		StringTokenizer strTok = new StringTokenizer ( projectLinks , "\n" );
		Vector < String > strVector = new Vector < String > ( );

		while ( strTok.hasMoreElements ( ) )
		{
			strVector.add ( ( String ) strTok.nextElement ( ) );
		}

		return strVector;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ust.downloader.BrowseInstructions#analyzeProjectsList(org.eclipse
	 * .swt.browser.Browser)
	 */

	/**
	 * 
	 * @return
	 */
	public String analyzeProjectsList ( )
	{
		String jsScript = " function loadProjectLinks() {"
				+ " var elements    =   document.getElementsByTagName('h2');"
				+ " if ( elements != null ) {"
				+ "      var hrefs       =   '';"
				+ "     for (i=0; i< elements.length; i++) {"
				+ "         var aTag    =   elements[i].childNodes[0];"
				+ "			if (aTag == null || aTag == undefined) "
				+ "			{	return hrefs;	}"
				+ "         var tName   =   aTag.tagName;"
				+ "         if ( tName == 'A' ||"
				+ "               tName == 'a' ) {"
				+ "             hrefs += aTag.href + '\\n';" + "            }"
				+ "      }" + "  }" + " return hrefs;" + "}"
				+ "return loadProjectLinks();";

		return jsScript;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ust.downloader.BrowseInstructions#lookForNextPageLink(org.eclipse
	 * .swt.browser.Browser, org.eclipse.swt.widgets.Display,
	 * org.ust.downloader.LockManager)
	 */

	/**
	 * 
	 * 
	 * 
	 * @return
	 * 
	 */
	public String lookForNextPageLink ( )
	{
		String nextButtonScript = "function probe(){"
				+ "var aForm = document.getElementsByClassName('results')[0].childNodes[1];"
				+ "var formChildrenNum = aForm.childNodes.length;"
				+ "var aNode = aForm.childNodes[formChildrenNum-2];"
				+ "if(aNode!=null)" + "{"
				+ "       if(aNode.nodeName=='A'||aNode.nadeName=='a')"
				+ "       {" + "               return aNode.href;" + "} else {"
				+ "       return null;" + "}}}return probe();";

		return nextButtonScript;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ust.downloader.BrowseInstructions#predictDownloadLink(java.lang.String
	 * )
	 */

	/**
	 * 
	 * 
	 * @param fileLink
	 * 
	 * @return
	 */
	public String predictDownloadLink ( String fileLink )
	{
		String resultStr = "http://downloads.sourceforge.net/project/";
		int indexOfFilesWord = fileLink.indexOf ( "/files/" );
		int lengthOfFilesWord = "/files/".length ( );
		int indexOfDownloadWord = fileLink.indexOf ( "/download" );
		String projectDirFileName = fileLink.substring ( indexOfFilesWord
				+ lengthOfFilesWord , indexOfDownloadWord );
		String projectName = fileLink.substring (
				"http://sourceforge.net/projects/".length ( ) ,
				indexOfFilesWord );

		resultStr += projectName;
		resultStr += "/";
		resultStr += projectDirFileName;
		resultStr += "?use_mirror=ncu";

		return resultStr;
	}

	/**
	 * @param initialProjectListUrl
	 *            the initialProjectListUrl to set
	 */
	public void setInitialProjectListUrl ( String initialProjectListUrl )
	{
		InitialProjectListUrl = initialProjectListUrl;
	}

	/**
	 * @return the initialProjectListUrl
	 */
	public String getInitialProjectListUrl ( )
	{
		return InitialProjectListUrl;
	}

	/**
	 * 
	 * 
	 * @param downloadFolderName
	 * 
	 */
	public void setDownloadFolderName ( String downloadFolderName )
	{
		this.downloadFolderName = downloadFolderName;
	}

	/**
	 * 
	 * 
	 * @return
	 */
	public String getDownloadFolderName ( )
	{
		return downloadFolderName;
	}

	/**
	 *
	 */
	public void postBeginAction ( )
	{
		// TODO Auto-generated method stub
	}

	/**
	 *
	 */
	public void preBeginAction ( )
	{
		// TODO Auto-generated method stub
	}

	/**
	 * 
	 * 
	 * @param eachProjectLink
	 * @param fileBrowser
	 * @param fileShell
	 * @param display
	 */
	public void duringNavigateProjectFilePage ( String eachProjectLink ,
			Browser fileBrowser , Shell fileShell , Display display )
	{
		// TODO Auto-generated method stub
	}

	/**
	 * 
	 * 
	 * @param eachProjectLink
	 * @param fileBrowser
	 * @param fileShell
	 * @param display
	 */
	public void duringNavigateProjectHomepage ( String eachProjectLink ,
			Browser fileBrowser , Shell fileShell , Display display )
	{
		// TODO Auto-generated method stub
	}

	/**
	 * 
	 * 
	 * @param targetUrl
	 * @param projectListBrowser
	 * @param shell
	 * @param display
	 */
	public void duringNavigateProjectList ( String targetUrl ,
			Browser projectListBrowser , Shell shell , Display display )
	{
		// TODO Auto-generated method stub
	}

	/**
	 * 
	 * 
	 * @param downloadLatestFile
	 */
	public void postDownloadFile ( String downloadLatestFile )
	{
		// TODO Auto-generated method stub
	}

	/**
	 * 
	 * 
	 * @param eachProjectLink
	 * @param fileBrowser
	 * @param fileShell
	 * @param display
	 */
	public void postNavigateProjectFilePage ( String eachProjectLink ,
			Browser fileBrowser , Shell fileShell , Display display )
	{
		// TODO Auto-generated method stub
	}

	/**
	 * 
	 * 
	 * @param eachProjectLink
	 * @param fileBrowser
	 * @param fileShell
	 * @param display
	 */
	public void postNavigateProjectHomepage ( String eachProjectLink ,
			Browser fileBrowser , Shell fileShell , Display display )
	{
		// TODO Auto-generated method stub
	}

	/**
	 * 
	 * 
	 * @param targetUrl
	 * @param projectListBrowser
	 * @param shell
	 * @param display
	 */
	public void postNavigateProjectList ( String targetUrl ,
			Browser projectListBrowser , Shell shell , Display display )
	{
		// TODO Auto-generated method stub
	}

	/**
	 * 
	 * 
	 * @param downloadLatestFile
	 */
	public void preDownloadFile ( String downloadLatestFile )
	{
		// TODO Auto-generated method stub
	}

	/**
	 * 
	 * 
	 * @param eachProjectLink
	 * @param fileBrowser
	 * @param fileShell
	 * @param display
	 */
	public void preNavigateProjectFilePage ( String eachProjectLink ,
			Browser fileBrowser , Shell fileShell , Display display )
	{
		// TODO Auto-generated method stub
	}

	/**
	 * 
	 * 
	 * @param eachProjectLink
	 * @param fileBrowser
	 * @param fileShell
	 * @param display
	 */
	public void preNavigateProjectHomepage ( String eachProjectLink ,
			Browser fileBrowser , Shell fileShell , Display display )
	{
		// TODO Auto-generated method stub
	}

	/**
	 * 
	 * 
	 * @param targetUrl
	 * @param projectListBrowser
	 * @param shell
	 * @param display
	 */
	public void preNavigateProjectList ( String targetUrl ,
			Browser projectListBrowser , Shell shell , Display display )
	{
		// TODO Auto-generated method stub
	}
}
